package org.white.word2pdf.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.white.word2pdf.service.Word2PdfService;
import org.white.word2pdf.utils.PdfUtil;

@AllArgsConstructor
@RestController("word2PdfController")
@RequestMapping("/word2pdf")
public class Word2PdfController {

    private final Word2PdfService service;

    @GetMapping
    public ModelAndView page() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("word2pdf");
        return modelAndView;
    }

    @PostMapping("/{type}/convert")
    public ResponseEntity<Void> word2pdf(@PathVariable("type") String type,
                                         @RequestParam("file") MultipartFile file) {
        try {
            service.convert(type, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(null);
    }

    @GetMapping(value = "itext", produces = {"application/json;charset=utf-8"})
    public String itext() {
        return "this is a itext";
    }

    @GetMapping(value = "doc4j/{name}", produces = {"application/json;charset=utf-8"})
    public String docx4j(@PathVariable String name) {
        float excTime = PdfUtil.word2pdfDocx4j(name);
        return "执行时间：" + excTime + "s";
    }

    @GetMapping(value = "onlyoffice/{name}", produces = {"application/json;charset=utf-8"})
    public String onlyoffice(@PathVariable String name) {
        float excTime = PdfUtil.word2pdfOnlfyoffice(name);
        return "从发送请求到下载文件的执行时间：" + excTime + "s";
    }

    @GetMapping(value = "word/{name}", produces = {"application/json;charset=utf-8"})
    public ResponseEntity downLoadFile(HttpServletResponse response, @PathVariable String name) {
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            response.setHeader("Accept-Ranges", "bytes");
            response.addHeader("Content-Disposition", "attachment;filename=\""
                    + URLEncoder.encode(name + ".docx", "UTF-8") + "\"");
            byte[] byteArr = PdfUtil.readFile(name);
            outputStream.write(byteArr);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new ResponseEntity(HttpStatus.OK);
    }

    @GetMapping(value = "aspose/{name}", produces = {"application/json;charset=utf-8"})
    public String aspose(@PathVariable String name) {
        float excTime = PdfUtil.word2pdfAspose(name);
        return "执行时间：" + excTime + "s";
    }

}
